

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pysiriproxy.packetPlayer &mdash; pysiriproxy v0.0.7 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.0.7',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="pysiriproxy v0.0.7 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">pysiriproxy v0.0.7 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pysiriproxy.packetPlayer</h1><div class="highlight"><pre>
<span class="c"># Copyright (C) (c) 2012 Brett Ponsler, Pete Lamonica, Pete Lamonica</span>
<span class="c"># This file is part of pysiriproxy.</span>
<span class="c">#</span>
<span class="c"># pysiriproxy is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pysiriproxy is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pysiriproxy.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;Contains the Player class.&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">pysiriproxy.utils</span> <span class="kn">import</span> <span class="n">toHex</span>
<span class="kn">from</span> <span class="nn">pysiriproxy.constants</span> <span class="kn">import</span> <span class="n">Modes</span>

<span class="kn">from</span> <span class="nn">pyamp.processes.threading</span> <span class="kn">import</span> <span class="n">Thread</span>


<div class="viewcode-block" id="Player"><a class="viewcode-back" href="../../_generated/pysiriproxy_packetPlayer.html#pysiriproxy.packetPlayer.Player">[docs]</a><span class="k">class</span> <span class="nc">Player</span><span class="p">(</span><span class="n">Thread</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The Player class loads a file containing data which it proceeds</span>
<span class="sd">    to send to the given protocol class using the same interface used</span>
<span class="sd">    to handle connections to the server. This allows us to save incoming</span>
<span class="sd">    data to the server and replay it for testing purposes.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">protocol</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">logger</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * protocol -- The server protocol class</span>
<span class="sd">        * filename -- The filename containing to data to replay</span>
<span class="sd">        * logger -- The logger</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__log</span> <span class="o">=</span> <span class="n">logger</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;PacketPlayer&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__protocol</span> <span class="o">=</span> <span class="n">protocol</span><span class="p">(</span><span class="n">logger</span><span class="o">=</span><span class="n">logger</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__content</span> <span class="o">=</span> <span class="nb">file</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__lines</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__content</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;-END_OF_DATA-&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__index</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="c"># Start in Line mode</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__setMode</span><span class="p">(</span><span class="n">Modes</span><span class="o">.</span><span class="n">Line</span><span class="p">)</span>

        <span class="c"># Map the fuction to call based on the current mode</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__modeMap</span> <span class="o">=</span> <span class="p">{</span><span class="n">Modes</span><span class="o">.</span><span class="n">Line</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__sendLineData</span><span class="p">,</span>
                          <span class="n">Modes</span><span class="o">.</span><span class="n">Raw</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__sendRawData</span><span class="p">}</span>

<div class="viewcode-block" id="Player.onCycle"><a class="viewcode-back" href="../../_generated/pysiriproxy_packetPlayer.html#pysiriproxy.packetPlayer.Player.onCycle">[docs]</a>    <span class="k">def</span> <span class="nf">onCycle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Called during each cycle of the thread.&#39;&#39;&#39;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__index</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__lines</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">shutdown</span><span class="p">()</span>
            <span class="k">return</span>

        <span class="c"># The first cycle corresponds to a created connection</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__protocol</span><span class="o">.</span><span class="n">connectionMade</span><span class="p">()</span>

        <span class="c"># Only send data once per second</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">10</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="c"># Call the data sending function for the current mode</span>
            <span class="n">modeFn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__modeMap</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__protocol</span><span class="o">.</span><span class="n">getMode</span><span class="p">())</span>
            <span class="k">if</span> <span class="n">modeFn</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">modeFn</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="Player.onShutdown"><a class="viewcode-back" href="../../_generated/pysiriproxy_packetPlayer.html#pysiriproxy.packetPlayer.Player.onShutdown">[docs]</a>    <span class="k">def</span> <span class="nf">onShutdown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Called in the event that the thread is shutdown.&#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__protocol</span><span class="o">.</span><span class="n">connectionLost</span><span class="p">(</span><span class="s">&quot;Player thread shutdown&quot;</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="Player.onException"><a class="viewcode-back" href="../../_generated/pysiriproxy_packetPlayer.html#pysiriproxy.packetPlayer.Player.onException">[docs]</a>    <span class="k">def</span> <span class="nf">onException</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;An exception occurred.</span>

<span class="sd">        * e -- The exception</span>
<span class="sd">        * traceback -- The traceback</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;Traceback:&quot;</span><span class="p">,</span> <span class="n">traceback</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">shutdown</span><span class="p">()</span>

    <span class="c">##### Private functions #####</span>
</div>
    <span class="k">def</span> <span class="nf">__sendLineData</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Send a line of data to the server.&#39;&#39;&#39;</span>
        <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__lines</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">__index</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__protocol</span><span class="o">.</span><span class="n">lineReceived</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__index</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">__sendRawData</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Send raw data to the server.&#39;&#39;&#39;</span>
        <span class="c"># True to send all remaining data, else to send parts</span>
        <span class="n">_SEND_ALL</span> <span class="o">=</span> <span class="bp">False</span>

        <span class="c"># The two ways of choosing data to send</span>
        <span class="k">if</span> <span class="n">_SEND_ALL</span><span class="p">:</span>
            <span class="c"># Send all remaining data at once</span>
            <span class="n">lines</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__lines</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">__index</span><span class="p">:]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__index</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__lines</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># Send parts of the remaining data</span>
            <span class="n">lines</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__lines</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">__index</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__index</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">lines</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__protocol</span><span class="o">.</span><span class="n">rawDataReceived</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__setMode</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Set the data sending mode.</span>

<span class="sd">        * mode -- The mode</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__mode</span> <span class="o">=</span> <span class="n">mode</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">pysiriproxy v0.0.7 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>